<?php


namespace App\Http\Controllers\Alipay;


use App\Models\AppletsIndexGoodsList;
use App\Models\AppletsIndexListCategory;
use App\Models\Collection;
use App\Services\CommonService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class MyCentreController extends BaseController
{
    /**
     * 个人中心推荐分类商品列表
     * @param Request $request
     * @return false|string
     */
    public function indexList(Request $request){
        $page = $request->input('page','1');
        $pageSize = $request->input('pageSize',AppletsIndexGoodsList::CACHE_LIMIT_NUM);
        $category = Redis::get(AppletsIndexListCategory::REDIS_FIRST_CATEGORY_KEY);
        $category = json_decode($category,true);
        if(empty($category)){
            $category = AppletsIndexListCategory::getFirstList();
        }
        $cateId = 0;
        if($category){
            $cateId = $category['id'];
        }
        $list = Redis::hget(AppletsIndexGoodsList::REDIS_LIST_KEY,$cateId.'-'.$page);
        if($list){
            $list = json_decode($list,true);
        }else{
            $list = AppletsIndexGoodsList::getList($cateId,$page,$pageSize);
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$list);
    }


    /**
     * 获取收藏商品列表
     */
    public function getCollection(Request $request){
        $page = $request->input('page','1');
        $pageSize = $request->input('pageSize',AppletsIndexGoodsList::CACHE_LIMIT_NUM);
        $user = $this->member;
        $collection = Collection::from('sp_collection_2 as c')->leftjoin('sp_goods_2 as g','g.id','c.goods_id')
            ->where([['c.uid',$user['id']],['c.is_delete',Collection::COLLECTION_NO_DELETE_STATUE],['g.is_delete',CommonService::GOODS_IS_DELETE_NOT]])
            ->select('c.id','c.goods_id','g.title','g.imgurl','g.min_price','g.min_lease_term')->orderBy('c.id','desc')->paginate($pageSize);;
        $collection = $collection ? $collection->toArray() : [];
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$collection);
    }


    /**
     * 删除收藏
     */
    public function delCollection(Request $request){
        $coll_id = $request->input('id');
        if(!empty($coll_id)){
            $user = $this->member;
            $res = Collection::where('uid',$user['id'])->wherein('id',explode(',',$coll_id))
                ->update(['is_delete'=>Collection::COLLECTION_IS_DELETE_STATUE]);
            if($res){
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'删除成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '删除失败');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要删除收藏的商品');
        }
    }

    /**
     * 通过商品id删除收藏
     */
    public function delCollectionByGoodsId(Request $request){
        $goods_id = $request->input('goods_id');
        if(!empty($goods_id)){
            $user = $this->member;
            if(Collection::where([['uid',$user['id']],['goods_id',$goods_id],['is_delete',Collection::COLLECTION_NO_DELETE_STATUE]])->count() > 0){
                $res = Collection::where([['uid',$user['id']],['goods_id',$goods_id]])
                    ->update(['is_delete'=>Collection::COLLECTION_IS_DELETE_STATUE]);
                if($res){
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'删除成功');
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '删除失败');
                }
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '商品收藏不存在或已删除');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要删除收藏的商品');
        }
    }

    /**
     * 添加收藏
     */
    public function addCollection(Request $request){
        $goods_id = $request->input('goods_id');
        if(!empty($goods_id)){
            $user = $this->member;
            $collection = Collection::where([['uid',$user['id']],['goods_id',$goods_id]])->select('id','is_delete')->first();
            $collection = $collection ? $collection->toArray() : [];
            if(!empty($collection)){
                if($collection['is_delete'] == Collection::COLLECTION_IS_DELETE_STATUE){
                    $res = Collection::where([['uid',$user['id']],['goods_id',$goods_id]])->update(['is_delete'=>Collection::COLLECTION_NO_DELETE_STATUE]);
                    if($res){
                        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'收藏成功');
                    }else{
                        return ReturnMsg(CommonService::HTTP_CODE_FAIL, '收藏失败');
                    }
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '已收藏，请勿重复操作');
                }
            }else{
                $data['uid'] = $user['id'];
                $data['goods_id'] = $goods_id;
                $data['addtime'] = time();
                $data['is_delete'] = Collection::COLLECTION_NO_DELETE_STATUE;
                $res = Collection::insert($data);
                if($res){
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'收藏成功');
                }else{
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '收藏失败');
                }
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '商品不存在');
        }
    }

    /**
     * 检测商品是否收藏
     */
    public function checkCollection(Request $request){
        $goods_id = $request->input('goods_id');
        if(!empty($goods_id)){
            $user = $this->member;
            $collection = Collection::where([['uid',$user['id']],['goods_id',$goods_id],['is_delete',Collection::COLLECTION_NO_DELETE_STATUE]])->count();
            $data['is_collection'] = $collection;
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '商品不存在');
        }
    }


}
